package edu.hut.bookshop.common;

import edu.hut.bookshop.Alipay.Alipay;
import edu.hut.bookshop.pojo.Pay;
import edu.hut.bookshop.service.OrderService;
import edu.hut.bookshop.service.PayService;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.math.BigDecimal;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ResponseBody;



/**
 * 支付统一处理控制器
 */


@RequestMapping("/alipay")
@Controller
public class AlipayController {

    @Autowired
    private PayService payService;

    @Autowired
    private OrderService orderService;

    /**
     * 支付宝异步通知接口
     * @param request
     * @return
     */
    @RequestMapping("/alipay_notify")
    @ResponseBody
    public String alipayNotify(HttpServletRequest request){
        //检查异步通知的签名是否合法
        System.out.println("进入支付宝异步通知接口！");
        if(!Alipay.isValid(request)){
            System.out.println("支付宝签名验证失败！");
            return "fail";
        }
        //表示签名验证成功
        //订单号
        String sn = request.getParameter("out_trade_no");
        System.out.println("sn="+sn);
        //支付宝交易号
        String paySn = request.getParameter("trade_no");
        System.out.println("paySn="+paySn);
        //支付金额
        String totalAmount = request.getParameter("total_amount");
        System.out.println("totalAmount"+totalAmount);
        //支付状态
        String status = request.getParameter("trade_status");
        System.out.println("status"+status);
        if(StringUtils.isEmpty(sn)){
            System.out.println("订单编号为空！");
            return "fail";
        }
        if(StringUtils.isEmpty(paySn)){
            System.out.println("支付宝交易号为空！");
            return "fail";
        }
        if(StringUtils.isEmpty(totalAmount)){
            System.out.println("支付金额为空！");
            return "fail";
        }
        if(StringUtils.isEmpty(status)){
            System.out.println("支付状态为空！");
            return "fail";
        }
        //到这表示参数都是合法的
        Pay pay = payService.findBysn(sn);
        if(pay == null){
            System.out.println("订单编号不存在！【" + sn + "】");
            return "fail";
        }
        if(pay.getTotalAmount().compareTo(new BigDecimal(totalAmount)) != 0){
            System.out.println("支付金额不同！【" + totalAmount + "】" + "【" + pay.getTotalAmount() + "】");
            return "fail";
        }
        if(!status.equals("TRADE_SUCCESS")){
            System.out.println("订单状态非成功！【" + status + "】");
            return "fail";
        }
        //表示一切合法
        System.out.println("状态为"+pay.getPstatus());
        if(pay.getPstatus() == Pay.pay_status_waiting){
            pay.setPaySn(paySn);
            pay.setPstatus(Pay.pay_status_paid);
            pay.setPayTime(new Date());
            payService.updatePay(sn,paySn,Pay.pay_status_paid,new Date());
            //orderService.updatestatus(pay.getOrderId());
            System.out.println(pay);
        }
        return "success";
    }
}
